using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using unvell.ReoGrid;
using unvell.ReoGrid.CellTypes;
using ZedGraph;

namespace ICP_MS.Plugin.HPLC;

internal class ReportClass : IDisposable
{
	private class ImageCell : CellBody
	{
		public Image image { get; set; }

		public ImageCell(Image image)
		{
			this.image = image;
		}

		public override void OnPaint(RGDrawingContext dc)
		{
			try
			{
				lock (image)
				{
					dc.Graphics.DrawImage(image, Bounds);
				}
				image.Dispose();
			}
			catch (Exception)
			{
			}
			dc.DrawCellText();
		}
	}

	private ExperimentDatas experiment = null;

	private ReoGridControl grid = null;

	private Worksheet sheetConc = null;

	private Worksheet sheetUcps = null;

	private Worksheet sheetCurve = null;

	private Worksheet sheetStd = null;

	private Worksheet sheetCond = null;

	private Worksheet sheetCps = null;

	private List<SamplesInfos> samples = new List<SamplesInfos>();

	private List<IsotopeInfos> isotopes = new List<IsotopeInfos>();

	private List<HPLCIntegerRegions> regions = new List<HPLCIntegerRegions>();

	private List<HPLCIsotopeCurve> curves = new List<HPLCIsotopeCurve>();

	private int regionIdx = 0;

	private int sampleIdx = 0;

	private MultiPrintDocument document = null;

	private int PageRows = 54;

	private int PageCols = 10;

	private int pageCount = 0;

	private int totalPages = 0;

	private ReportConfig config = new ReportConfig();

	private ManualResetEvent waitDispose = new ManualResetEvent(initialState: false);

	private float fzHeader = 9.5f;

	private float fzTitle = 10.5f;

	private float fzFooter = 9.5f;

	private float fzBody = 8f;

	public ReportConfig Config
	{
		get
		{
			return config;
		}
		set
		{
			config = value;
		}
	}

	public MultiPrintDocument Document => document;

	public int TotalPages => totalPages;

	public bool IsDisposed { get; set; }

	public event EventHandler BeginCreate;

	public event EventHandler EndCreate;

	public ReportClass(ExperimentDatas datas)
	{
		experiment = datas;
	}

	public void CreateDocument()
	{
		if (this.BeginCreate != null)
		{
			this.BeginCreate(null, EventArgs.Empty);
		}
		Task.Factory.StartNew(delegate
		{
			try
			{
				totalPages = 0;
				SetupWorksheet();
				SetupCondition();
				SetupCurves();
				SetupContents();
				SetupDocument();
			}
			catch (Exception ex)
			{
				document = null;
				MessageBox.Show(ex.Message);
			}
			finally
			{
				if (this.EndCreate != null)
				{
					this.EndCreate(null, EventArgs.Empty);
				}
				waitDispose.WaitOne();
				Dispose();
			}
		});
	}

	private void SetupDocument()
	{
		List<PrintDocument> documents = new List<PrintDocument>();
		if (config.Cond)
		{
			documents.Add(sheetCond.CreatePrintDocument());
		}
		if (config.Curve)
		{
			documents.Add(sheetCurve.CreatePrintDocument());
		}
		if (config.Conc)
		{
			documents.Add(sheetConc.CreatePrintDocument());
		}
		if (config.Ucps)
		{
			documents.Add(sheetUcps.CreatePrintDocument());
		}
		if (config.Cps)
		{
			documents.Add(sheetCps.CreatePrintDocument());
		}
		if (documents.Count > 0)
		{
			document = new MultiPrintDocument(documents.ToArray());
			document.DocumentName = totalPages.ToString();
			document.EndPrint += delegate
			{
				waitDispose.Set();
			};
		}
		else
		{
			document = null;
		}
	}

	private void SetPageSettings(Worksheet sheet)
	{
		sheet.PageSettings.Margins.Left = 59;
		sheet.PageSettings.Margins.Right = 59;
		sheet.PageSettings.Margins.Top = 20;
		sheet.PageSettings.Margins.Bottom = 20;
	}

	private void SetupWorksheet()
	{
		grid = new ReoGridControl();
		Worksheet sheet = grid.CurrentWorksheet;
		sheet.SetCols(10);
		sheet.GetRangeStyle(ReoGridRange.EntireRange).HAlign = ReoGridHorAlign.Center;
		sheet.GetRangeStyle(ReoGridRange.EntireRange).VAlign = ReoGridVerAlign.Middle;
		int sheetIndex = 1;
		if (config.Conc)
		{
			sheetConc = grid.CopyWorksheet(sheet, sheetIndex++, "Conc");
			SetPageSettings(sheetConc);
		}
		if (config.Ucps)
		{
			sheetUcps = grid.CopyWorksheet(sheet, sheetIndex++, "UCPS");
			SetPageSettings(sheetUcps);
		}
		if (config.Cps)
		{
			sheetCps = grid.CopyWorksheet(sheet, sheetIndex++, "CPS");
			SetPageSettings(sheetCps);
		}
		if (config.Cond)
		{
			sheetCond = grid.CopyWorksheet(sheet, sheetIndex++, "Condition");
			SetPageSettings(sheetCond);
		}
		if (config.Curve)
		{
			sheetCurve = grid.CopyWorksheet(sheet, sheetIndex++, "Curve");
			SetPageSettings(sheetCurve);
		}
		if (config.Std)
		{
			sheetStd = grid.CopyWorksheet(sheet, sheetIndex++, "Std");
			SetPageSettings(sheetStd);
		}
		PageRows = 56;
		PageCols = 10;
	}

	private void SetupPageHeader(Worksheet sheet, int row)
	{
		sheet.SetRows(row + PageRows);
		if (config.Logo != null)
		{
			sheet.MergeRange(row, 0, 3, 2);
			Image image = config.Logo.Clone() as Image;
			sheet[row, 0] = new ImageCell(image);
		}
		if (config.Header != "")
		{
			sheet.MergeRange(row, 2, 1, PageCols - 2);
			sheet[row, 2] = config.Header;
		}
		if (config.Title != "")
		{
			sheet.MergeRange(row + 1, 2, 3, PageCols - 2);
			sheet.Ranges[row + 1, 2, 3, PageCols - 2].Style.FontSize = 14f;
			sheet[row + 1, 2] = config.Title;
		}
	}

	private void SetupCondition()
	{
		if (config.Cond)
		{
			pageCount = 0;
			SetupPageCondition(0);
			pageCount++;
			totalPages += pageCount;
		}
	}

	private void SetupStd()
	{
	}

	private void SetupCurves()
	{
		if (!config.Curve)
		{
			return;
		}
		pageCount = 0;
		curves = new List<HPLCIsotopeCurve>();
		foreach (IsotopeInfos isotope in experiment.Isotopes)
		{
			if (!isotope.visible || isotope.inter)
			{
				continue;
			}
			foreach (HPLCIsotopeCurve curve in experiment.Curves)
			{
				if (curve.symbol == isotope.symbol && curve.calculated)
				{
					curves.Add(curve);
					break;
				}
			}
		}
		pageCount = 0;
		int curveNums;
		for (int curveIdx = 0; curveIdx < curves.Count; curveIdx += curveNums)
		{
			int rowNums = 0;
			curveNums = 0;
			rowNums += 7;
			for (int i = curveIdx; i < curves.Count; i++)
			{
				rowNums += 15 + curves[i].points;
				if (rowNums <= PageRows)
				{
					curveNums++;
					continue;
				}
				break;
			}
			SetupPageCurve(pageCount * PageRows, curveIdx, curveNums);
			pageCount++;
		}
		totalPages += pageCount;
	}

	private Image CreateCurve(double[] x1, double[] y1, double[] x2, double[] y2, string symbol)
	{
		Image image = null;
		try
		{
			using ZedGraphControl graph = new ZedGraphControl();
			GraphPane pane = graph.GraphPane;
			pane.XAxis.Title.Text = "浓度";
			pane.YAxis.Title.Text = "强度";
			pane.Title.Text = symbol;
			LineItem line = pane.AddCurve("", x1, y1, Color.Blue, SymbolType.Circle);
			line.Line.IsVisible = false;
			pane.AddCurve("", x2, y2, Color.Black, SymbolType.None);
			pane.AxisChange();
			image = pane.GetImage(1101, 489, 96f);
		}
		catch (Exception ex)
		{
			image = null;
			Log.InsertLog("数据报表", "创建曲线失败", ex.Message, (Severity)3);
		}
		return image;
	}

	private void SetupPageCondition(int currentRow)
	{
		SetupPageHeader(sheetCond, currentRow);
		currentRow += 5;
		sheetCond.Ranges[currentRow, 0, 1, PageCols].Style.BackColor = Color.FromKnownColor(KnownColor.Control);
		sheetCond[currentRow, 0] = "Ex";
		sheetCond[currentRow, 1] = "D2";
		sheetCond[currentRow, 2] = "Pole";
		sheetCond[currentRow, 3] = "D1";
		sheetCond[currentRow, 4] = "DA";
		sheetCond[currentRow, 5] = "QE";
		sheetCond[currentRow, 6] = "F";
		sheetCond[currentRow, 7] = "L1";
		sheetCond[currentRow, 8] = "L2";
		sheetCond[currentRow, 9] = "L3";
		currentRow++;
		sheetCond[currentRow, 0] = experiment.CurrentLens.Ex.ToString("F2");
		sheetCond[currentRow, 1] = experiment.CurrentLens.D2.ToString("F2");
		sheetCond[currentRow, 2] = experiment.CurrentLens.Pole.ToString("F2");
		sheetCond[currentRow, 3] = experiment.CurrentLens.D1.ToString("F2");
		sheetCond[currentRow, 4] = experiment.CurrentLens.DA.ToString("F2");
		sheetCond[currentRow, 5] = experiment.CurrentLens.QE.ToString("F2");
		sheetCond[currentRow, 6] = experiment.CurrentLens.F.ToString("F2");
		sheetCond[currentRow, 7] = experiment.CurrentLens.L1.ToString("F2");
		sheetCond[currentRow, 8] = experiment.CurrentLens.L2.ToString("F2");
		sheetCond[currentRow, 9] = experiment.CurrentLens.L3.ToString("F2");
		currentRow++;
		sheetCond.Ranges[currentRow, 0, 1, PageCols].Style.BackColor = Color.FromKnownColor(KnownColor.Control);
		sheetCond[currentRow, 0] = "Aux.";
		sheetCond[currentRow, 1] = "Neub.";
		sheetCond[currentRow, 2] = "Cool";
		sheetCond[currentRow, 3] = "X";
		sheetCond[currentRow, 4] = "Y";
		sheetCond[currentRow, 5] = "Z";
		currentRow++;
		sheetCond[currentRow, 0] = experiment.CurrentPlasma.Auxiliary.ToString("F2");
		sheetCond[currentRow, 1] = experiment.CurrentPlasma.Nebuliser.ToString("F2");
		sheetCond[currentRow, 2] = experiment.CurrentPlasma.Cool.ToString("F2");
		sheetCond[currentRow, 3] = experiment.CurrentPlat.X.ToString("F2");
		sheetCond[currentRow, 4] = experiment.CurrentPlat.Y.ToString("F2");
		sheetCond[currentRow, 5] = experiment.CurrentPlat.Z.ToString("F2");
		sheetCond.MergeRange(PageRows - 3, 0, 1, PageCols);
		sheetCond[PageRows - 3, 0] = "分析条件--" + (pageCount + 1);
	}

	private void SetupPageCurve(int currentRow, int curveIdx, int curveNums)
	{
		int startRow = currentRow;
		SetupPageHeader(sheetCurve, currentRow);
		currentRow += 5;
		for (int i = 0; i < curveNums; i++)
		{
			HPLCIsotopeCurve curve = curves[curveIdx + i];
			List<HPLCStatisticResults> results = (from result in experiment.Statistics
				from sample in experiment.Samples
				where result.symbol == curve.symbol && result.regionLabel == curve.regionLabel && result.stdCon >= 0.0 && result.sampleId == sample.guid && (sample.type == 0 || sample.type == curve.method) && sample.status == 1
				select result).ToList();
			results.Sort((HPLCStatisticResults r0, HPLCStatisticResults r1) => r0.stdCon.CompareTo(r1.stdCon));
			currentRow++;
			IEnumerable<double> cps1 = from result in results
				where result.include
				select result.avgCPS;
			IEnumerable<double> con1 = from result in results
				where result.include
				select result.stdCon;
			double[] con2 = con1.ToArray();
			double[] cps2 = new double[con2.Length];
			for (int j = 0; j < con2.Length; j++)
			{
				cps2[j] = con2[j] * curve.k + curve.b;
			}
			Image image = CreateCurve(con1.ToArray(), cps1.ToArray(), con2.ToArray(), cps2.ToArray(), curve.symbol);
			if (image != null)
			{
				sheetCurve.MergeRange(currentRow, 1, 12, PageCols - 2);
				sheetCurve[new ReoGridRange(currentRow, 1, 12, PageCols - 2)] = new ImageCell(image);
			}
			currentRow += 12;
			string formular = $"y = {curve.k:0.00} * x + {curve.b:0.00}; R2 = {curve.r:0.00000}";
			sheetCurve.MergeRange(currentRow, 1, 1, PageCols - 2);
			sheetCurve[currentRow, 1] = formular;
			currentRow++;
			sheetCurve.Ranges[currentRow, 2, 1, 5].Data = new string[5] { "样品名", "标准浓度", "计算浓度", "CPS", "参与计算" };
			sheetCurve.Ranges[currentRow, 2, 1, 5].Style.BackColor = Color.FromKnownColor(KnownColor.Control);
			currentRow++;
			foreach (HPLCStatisticResults result2 in results)
			{
				sheetCurve.Ranges[currentRow++, 2, 1, 5].Data = new string[5]
				{
					result2.label,
					result2.stdCon.ToString("F3"),
					result2.avgCon.ToString("F3"),
					result2.avgCPS.ToString("F0"),
					result2.include.ToString()
				};
			}
		}
		sheetCurve.MergeRange(startRow + PageRows - 3, 0, 1, PageCols);
		sheetCurve[startRow + PageRows - 3, 0] = "工作曲线--" + (pageCount + 1);
	}

	private void SetupContents()
	{
		if (!config.Conc && !config.Cps && !config.Ucps)
		{
			return;
		}
		samples = experiment.Samples.Where((SamplesInfos sample) => sample.report).ToList();
		regions = experiment.IntegerRegions.Select((HPLCIntegerRegions region) => region).ToList();
		pageCount = 0;
		for (regionIdx = 0; regionIdx < regions.Count; regionIdx += PageCols - 1)
		{
			int regionNums = Math.Min(PageCols - 1, regions.Count - regionIdx);
			sampleIdx = 0;
			while (sampleIdx < samples.Count)
			{
				int rowNums = 0;
				int sampleNums = 0;
				rowNums += 8;
				for (int i = sampleIdx; i < samples.Count; i++)
				{
					rowNums++;
					if (experiment.ReportParams.ShowRuns)
					{
						rowNums += samples[i].mains;
					}
					rowNums++;
					if (experiment.ReportParams.ShowSd)
					{
						rowNums++;
					}
					if (experiment.ReportParams.ShowRsd)
					{
						rowNums++;
					}
					if (rowNums <= PageRows)
					{
						sampleNums++;
						continue;
					}
					break;
				}
				SetupContPage(pageCount * PageRows, sampleNums, regionNums);
				pageCount++;
				sampleIdx += sampleNums;
			}
		}
		if (config.Conc)
		{
			totalPages += pageCount;
		}
		if (config.Cps)
		{
			totalPages += pageCount;
		}
		if (config.Ucps)
		{
			totalPages += pageCount;
		}
	}

	private void SetupContPage(int currentRow, int sampleNums, int regionNums)
	{
		int startRow = currentRow;
		ContSetPageHeader(currentRow);
		currentRow += 5;
		ContSetHeader(currentRow);
		for (int j = 0; j < regionNums; j++)
		{
			HPLCIntegerRegions region3 = regions[j + regionIdx];
			ContSetRegion(currentRow, j + 1, region3.Label);
		}
		currentRow++;
		for (int l = 0; l < sampleNums; l++)
		{
			SamplesInfos sample = samples[sampleIdx + l];
			ContSetSampleName(currentRow, regionNums, sample.label + "    " + sample.time.ToString("dd/MM/yyyy HH:mm:ss"));
			currentRow++;
			if (experiment.ReportParams.ShowRuns)
			{
				for (int m = 0; m < sample.mains; m++)
				{
					ContSetNumber(currentRow, m);
					for (int k = 0; k < regionNums; k++)
					{
						HPLCIntegerRegions region2 = regions[k + regionIdx];
						HPLCAnalyseInfos value2 = experiment.Results.Where((HPLCAnalyseInfos result) => result.sampleId == sample.guid && result.symbol == region2.symbol && result.regionLabel == region2.ReferenceId && result.number == m).First();
						bool inter2 = experiment.Isotopes.Where((IsotopeInfos isotope) => isotope.symbol == region2.symbol).First().inter;
						ContSetResult(m + currentRow, k + 1, value2, inter2);
					}
				}
				currentRow += sample.mains;
			}
			ContSetStatHeader(currentRow, regionNums);
			for (int i = 0; i < regionNums; i++)
			{
				HPLCIntegerRegions region = regions[i + regionIdx];
				HPLCStatisticResults value = experiment.Statistics.Where((HPLCStatisticResults result) => result.sampleId == sample.guid && result.symbol == region.symbol && result.regionLabel == region.ReferenceId).First();
				bool inter = experiment.Isotopes.Where((IsotopeInfos isotope) => isotope.symbol == region.symbol).First().inter;
				ContSetStatResult(currentRow, i + 1, value, inter);
			}
			if (experiment.ReportParams.ShowMean)
			{
				currentRow++;
			}
			if (experiment.ReportParams.ShowSd)
			{
				currentRow++;
			}
			if (experiment.ReportParams.ShowRsd)
			{
				currentRow++;
			}
		}
		ContSetFistColumnBackground(startRow + 5, currentRow - startRow - 5);
		ContSetFooter(startRow + PageRows - 1);
		currentRow += 2;
	}

	private void ContSetPageHeader(int row)
	{
		if (config.Conc)
		{
			SetupPageHeader(sheetConc, row);
		}
		if (config.Cps)
		{
			SetupPageHeader(sheetCps, row);
		}
		if (config.Ucps)
		{
			SetupPageHeader(sheetUcps, row);
		}
	}

	private void ContSetHeader(int row)
	{
		if (config.Conc)
		{
			sheetConc.MergeRange(row, 0, 1, 1);
			sheetConc[row, 0] = "样品\\元素";
			sheetConc.Ranges[row, 1, 1, PageCols - 1].Style.BackColor = Color.FromKnownColor(KnownColor.Control);
			sheetConc.Ranges[row, 1, 1, PageCols - 1].Style.Bold = true;
		}
		if (config.Cps)
		{
			sheetCps.MergeRange(row, 0, 1, 1);
			sheetCps[row, 0] = "样品\\元素";
			sheetCps.Ranges[row, 1, 1, PageCols - 1].Style.BackColor = Color.FromKnownColor(KnownColor.Control);
			sheetCps.Ranges[row, 1, 1, PageCols - 1].Style.Bold = true;
		}
		if (config.Ucps)
		{
			sheetUcps.MergeRange(row, 0, 1, 1);
			sheetUcps[row, 0] = "样品\\元素";
			sheetUcps.Ranges[row, 1, 1, PageCols - 1].Style.BackColor = Color.FromKnownColor(KnownColor.Control);
			sheetUcps.Ranges[row, 1, 1, PageCols - 1].Style.Bold = true;
		}
	}

	private void ContSetRegion(int row, int col, string regionLabel)
	{
		if (config.Conc)
		{
			sheetConc[row, col] = regionLabel;
		}
		if (config.Ucps)
		{
			sheetUcps[row, col] = regionLabel;
		}
		if (config.Cps)
		{
			sheetCps[row, col] = regionLabel;
		}
	}

	private void ContSetSampleName(int row, int cols, string label)
	{
		if (config.Conc)
		{
			sheetConc.MergeRange(row, 1, 1, PageCols - 1);
			sheetConc.Cells[row, 1].Style.HAlign = ReoGridHorAlign.Left;
			sheetConc[row, 1] = label;
			sheetConc.SetRangeBorders(new ReoGridRange(row, 1, 1, cols), BorderPositions.Bottom, new unvell.ReoGrid.BorderStyle(Color.Black, BorderLineStyle.BoldSolid));
		}
		if (config.Cps)
		{
			sheetCps.MergeRange(row, 1, 1, PageCols - 1);
			sheetCps.Cells[row, 1].Style.HAlign = ReoGridHorAlign.Left;
			sheetCps[row, 1] = label;
			sheetCps.SetRangeBorders(new ReoGridRange(row, 1, 1, cols), BorderPositions.Bottom, new unvell.ReoGrid.BorderStyle(Color.Black, BorderLineStyle.BoldSolid));
		}
		if (config.Ucps)
		{
			sheetUcps.MergeRange(row, 1, 1, PageCols - 1);
			sheetUcps.Cells[row, 1].Style.HAlign = ReoGridHorAlign.Left;
			sheetUcps[row, 1] = label;
			sheetUcps.SetRangeBorders(new ReoGridRange(row, 1, 1, cols), BorderPositions.Bottom, new unvell.ReoGrid.BorderStyle(Color.Black, BorderLineStyle.BoldSolid));
		}
	}

	private void ContSetNumber(int row, int m)
	{
		if (config.Conc)
		{
			sheetConc[m + row, 0] = (m + 1).ToString();
		}
		if (config.Cps)
		{
			sheetCps[m + row, 0] = (m + 1).ToString();
		}
		if (config.Ucps)
		{
			sheetUcps[m + row, 0] = (m + 1).ToString();
		}
	}

	private void ContSetResult(int row, int col, HPLCAnalyseInfos result, bool inter)
	{
		if (config.Conc)
		{
			if (inter)
			{
				sheetConc[row, col] = string.Format(new FormatPercent(), "{0:U}", result.concentration);
				sheetConc.Cells[row, col].Style.BackColor = Color.Yellow;
			}
			else
			{
				sheetConc[row, col] = string.Format(new FormatValue(), "{0:U}", result.concentration);
			}
			if (result.exclude)
			{
				sheetConc.Cells[row, col].Style.TextColor = Color.Red;
			}
		}
		if (config.Cps)
		{
			if (inter)
			{
				sheetCps[row, col] = string.Format(new FormatPercent(), "{0:U}", result.icps);
				sheetCps.Cells[row, col].Style.BackColor = Color.Yellow;
			}
			else
			{
				sheetCps[row, col] = string.Format(new FormatValue(), "{0:U}", result.icps);
			}
			if (result.exclude)
			{
				sheetCps.Cells[row, col].Style.TextColor = Color.Red;
			}
		}
		if (config.Ucps)
		{
			if (inter)
			{
				sheetUcps.Cells[row, col].Style.BackColor = Color.Yellow;
			}
			sheetUcps[row, col] = string.Format(new FormatValue(), "{0:U}", result.intensity);
			if (result.exclude)
			{
				sheetUcps.Cells[row, col].Style.TextColor = Color.Red;
			}
		}
	}

	private void ContSetStatHeaderSingle(Worksheet sheet, int row, int cols)
	{
		if (experiment.ReportParams.ShowMean)
		{
			sheet.Ranges[row, 1, 1, PageCols - 1].Style.Bold = true;
			sheet.Ranges[row, 1, 1, PageCols - 1].Style.TextColor = Color.Blue;
			sheet.SetRangeBorders(new ReoGridRange(row, 1, 1, cols), BorderPositions.Bottom, new unvell.ReoGrid.BorderStyle(Color.Black, BorderLineStyle.Solid));
			sheet[row++, 0] = "χ";
		}
		if (experiment.ReportParams.ShowSd)
		{
			sheet.Ranges[row, 1, 1, PageCols - 1].Style.TextColor = Color.Blue;
			sheet[row++, 0] = "σ";
		}
		if (experiment.ReportParams.ShowRsd)
		{
			sheet.Ranges[row, 1, 1, PageCols - 1].Style.TextColor = Color.Blue;
			sheet[row++, 0] = "RSD%";
		}
	}

	private void ContSetStatHeader(int row, int cols)
	{
		if (config.Conc)
		{
			ContSetStatHeaderSingle(sheetConc, row, cols);
		}
		if (config.Cps)
		{
			ContSetStatHeaderSingle(sheetCps, row, cols);
		}
		if (config.Ucps)
		{
			ContSetStatHeaderSingle(sheetUcps, row, cols);
		}
	}

	private void ContSetStatResult(int row, int col, HPLCStatisticResults result, bool inter)
	{
		if (config.Conc)
		{
			int concRow = row;
			if (experiment.ReportParams.ShowMean)
			{
				if (inter)
				{
					sheetConc.Ranges[concRow, col, 1, 1].Style.BackColor = Color.Yellow;
					sheetConc[concRow++, col] = string.Format(new FormatPercent(), "{0:U}", result.avgCon);
				}
				else
				{
					sheetConc[concRow++, col] = string.Format(new FormatValue(), "{0:U}", result.avgCon);
				}
			}
			if (experiment.ReportParams.ShowSd)
			{
				if (inter)
				{
					sheetConc.Ranges[concRow, col, 1, 1].Style.BackColor = Color.Yellow;
					sheetConc[concRow++, col] = string.Format(new FormatPercent(), "{0:U}", result.sdCon);
				}
				else
				{
					sheetConc[concRow++, col] = string.Format(new FormatValue(), "{0:U}", result.sdCon);
				}
			}
			if (experiment.ReportParams.ShowRsd)
			{
				if (inter)
				{
					sheetConc.Ranges[concRow, col, 1, 1].Style.BackColor = Color.Yellow;
				}
				sheetConc[concRow++, col] = string.Format(new FormatValue(), "{0:U}", result.rsdCon);
			}
		}
		if (config.Cps)
		{
			int cpsRow = row;
			if (experiment.ReportParams.ShowMean)
			{
				if (inter)
				{
					sheetCps.Ranges[cpsRow, col, 1, 1].Style.BackColor = Color.Yellow;
					sheetCps[cpsRow++, col] = string.Format(new FormatPercent(), "{0:U}", result.avgCPS);
				}
				else
				{
					sheetCps[cpsRow++, col] = string.Format(new FormatValue(), "{0:U}", result.avgCPS);
				}
			}
			if (experiment.ReportParams.ShowSd)
			{
				if (inter)
				{
					sheetCps.Ranges[cpsRow, col, 1, 1].Style.BackColor = Color.Yellow;
					sheetCps[cpsRow++, col] = string.Format(new FormatPercent(), "{0:U}", result.sdCPS);
				}
				else
				{
					sheetCps[cpsRow++, col] = string.Format(new FormatValue(), "{0:U}", result.sdCPS);
				}
			}
			if (experiment.ReportParams.ShowRsd)
			{
				if (inter)
				{
					sheetCps.Ranges[cpsRow, col, 1, 1].Style.BackColor = Color.Yellow;
				}
				sheetCps[cpsRow++, col] = string.Format(new FormatValue(), "{0:U}", result.rsdCPS);
			}
		}
		if (!config.Ucps)
		{
			return;
		}
		int ucpsRow = row;
		if (experiment.ReportParams.ShowMean)
		{
			if (inter)
			{
				sheetUcps.Ranges[ucpsRow, col, 1, 1].Style.BackColor = Color.Yellow;
			}
			sheetUcps[ucpsRow++, col] = string.Format(new FormatValue(), "{0:U}", result.avgUICPS);
		}
		if (experiment.ReportParams.ShowSd)
		{
			if (inter)
			{
				sheetUcps.Ranges[ucpsRow, col, 1, 1].Style.BackColor = Color.Yellow;
			}
			sheetUcps[ucpsRow++, col] = string.Format(new FormatValue(), "{0:U}", result.sdUICPS);
		}
		if (experiment.ReportParams.ShowRsd)
		{
			if (inter)
			{
				sheetUcps.Ranges[ucpsRow, col, 1, 1].Style.BackColor = Color.Yellow;
			}
			sheetUcps[ucpsRow++, col] = string.Format(new FormatValue(), "{0:U}", result.rsdUICPS);
		}
	}

	private void ContSetFistColumnBackground(int startRow, int endRow)
	{
		if (config.Conc)
		{
			sheetConc.Ranges[startRow, 0, endRow, 1].Style.BackColor = Color.FromKnownColor(KnownColor.Control);
		}
		if (config.Cps)
		{
			sheetCps.Ranges[startRow, 0, endRow, 1].Style.BackColor = Color.FromKnownColor(KnownColor.Control);
		}
		if (config.Ucps)
		{
			sheetUcps.Ranges[startRow, 0, endRow, 1].Style.BackColor = Color.FromKnownColor(KnownColor.Control);
		}
	}

	private void ContSetFooter(int row)
	{
		if (config.Conc)
		{
			sheetConc.MergeRange(row, 0, 1, PageCols);
			sheetConc[row, 0] = $"计算浓度--{pageCount + 1}";
			sheetConc.Ranges[row - PageRows + 4, 0, PageRows - 8, PageCols].Style.FontSize = 8f;
		}
		if (config.Cps)
		{
			sheetCps.MergeRange(row, 0, 1, PageCols);
			sheetCps[row, 0] = $"校正强度--{pageCount + 1}";
			sheetCps.Ranges[row - PageRows + 4, 0, PageRows - 8, PageCols].Style.FontSize = 8f;
		}
		if (config.Ucps)
		{
			sheetUcps.MergeRange(row, 0, 1, PageCols);
			sheetUcps[row, 0] = $"原始强度--{pageCount + 1}";
			sheetUcps.Ranges[row - PageRows + 4, 0, PageRows - 8, PageCols].Style.FontSize = 8f;
		}
	}

	public void Dispose()
	{
		if (!IsDisposed)
		{
			IsDisposed = true;
			if (document != null)
			{
				document.Dispose();
			}
			if (grid != null)
			{
				grid.Reset();
				grid.Dispose();
			}
			curves = null;
			samples = null;
			isotopes = null;
			regions = null;
		}
	}
}
